import 'package:aoe_flutter/service/login_control.dart';
import 'package:aoe_flutter/widgets/menu/cate_card.dart';
import 'package:flutter/material.dart';

import '../../app.dart';
import '../../basic_app.dart';
import 'bundle.dart';

class BundleBus {
  static Map<String, Map<String, Bundle>> _pool = {};
  static Map<String, Bundle> allBundles = {};

  static Bundle register(String groupName, Bundle bundle,
      {String type = 'grid'}) {
    allBundles.putIfAbsent(bundle.id, () => bundle);
    Map<String, Bundle> bundleMap = _pool.putIfAbsent(groupName, () => {});
    return bundleMap.putIfAbsent(bundle.id, () => bundle);
  }

  static Map<String, List<Bundle>> get group {
    UserInfo _userInfo = APP.getInstance().userInfo;
    Map<String, List<Bundle>> groupingBundles = {};
    if (_userInfo != null) {
      List bundleIds = _userInfo.bundles;
      // List bundleIds = [];
      _pool.entries.forEach((entry) {
        List<Bundle> bundles = entry.value.values
            .where(
                (bundle) => app.menuFree ? true : bundleIds.contains(bundle.id))
            .toList();
        bundles
            .sort((bundle1, bundle2) => bundle1.sort > bundle2.sort ? 1 : -1);
        if (bundles.length > 0)
          groupingBundles.putIfAbsent(entry.key, () => bundles);
      });
    }
    return groupingBundles;
  }

  static List<Widget> groupingMenus(BuildContext context) {
    return BundleBus.group.entries.map((entry) {
      List<Bundle> bundles = entry.value;
      String groupName = entry.key;
      if (bundles.length > 0)
        return Container(
          // margin: EdgeInsets.all(6),
          // padding: EdgeInsets.all(6),
          decoration: BoxDecoration(
              color: Theme.of(context).cardColor,
              borderRadius: BorderRadius.vertical(top: Radius.circular(5))),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Padding(
                padding: EdgeInsets.symmetric(vertical:2, horizontal: 10),
                child:
                    // Text(
                    //   groupName,
                    //   style: Theme.of(context).textTheme.subhead,
                    // ),
                    Container(
                  alignment: Alignment.bottomLeft,
                  // width: screenWidth - 20,
                  padding: const EdgeInsets.symmetric(
                      vertical: 5.0, horizontal: 5.0),
                  height: 35.0,
                  child: Text(
                    groupName,
                    style: TextStyle(
                      color: Theme.of(context).primaryColor,
                      fontSize: 18.0,
                    ),
                  ),
                ),
              ),
              Divider(
                height: 5,
              ),
              Wrap(
                children: _buildBundleWidget(bundles, context),
              ),
            ],
          ),
        );
      else
        return Container();
    }).toList();
  }

  static List<Widget> _buildBundleWidget(
      List<Bundle> bundles, BuildContext context,
      {bool shortcut = false}) {
    List<Widget> widgetList = [];
    // for (var item in allBundles.keys) {
      widgetList.add(CateCard(name: "审批", bundles: bundles));
    // }

    return widgetList;
  }
}
